home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / i / internet / software / tnftpsr / resolv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-30  |  2.3 KB  |  108 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <tos.h>
  5. #include <ext.h>
  6. #include <aes.h>
  7. #include "cookie.h"
  8. #include "time.h"
  9. #include "tndefs.h"
  10. #include "tcpdef.h"
  11. #include "inetcust.h"
  12. #include "name.h"
  13.  
  14. extern void w_info(struct wi_str *wp, char *name);
  15. #define noDEBUG
  16.  
  17. long resolv(char *host, char ipaddr[4],struct wi_str *wp)
  18. {
  19. INETCUST *custom;
  20. COOKIE     *cookie;
  21. int udp,a[4],i;
  22. NMITEM    *nm;
  23. long    timeout;
  24. DESTI   dest;
  25. long    got;
  26. char buf[512];
  27.  
  28.     if(host && sscanf(host,"%ld.",&got) == 1)
  29.     {
  30.       if(sscanf(host,"%3d.%3d.%3d.%3d",&a[0],&a[1],&a[2],&a[3]) == 4)
  31.       {
  32.             ipaddr[0] = a[0];
  33.             ipaddr[1] = a[1];
  34.             ipaddr[2] = a[2];
  35.             ipaddr[3] = a[3];
  36.             return 0L;
  37.       }
  38.       else return -1L;
  39.     }
  40.     if(!GetIPAddr(host,ipaddr))
  41.         return 0L;
  42.     if(!host || !strlen(host))
  43.         return -1L;
  44.         if(GetIPAddr("NAMESERVER",dest.IPAddr))
  45.         {
  46.      cookie = get_cookie(INETCUSTCOOKIE);
  47.      if(!cookie || (custom = (INETCUST *)(cookie->val))==NULL)
  48.      {
  49.                 w_info(wp,"<Error: no name server specified.>");
  50.         return(-1L);
  51.      }
  52.     
  53.      if(!custom->nameserver[0])
  54.      {
  55.                 w_info(wp,"<Error: no name server specified.>");
  56.         return(-1L);
  57.      }
  58.     
  59.      *(INADDR *)dest.IPAddr = custom->nameserver[0];
  60.         }
  61.     dest.Port = NAMESERVPORT;
  62.  
  63.     nm = (NMITEM *)buf;
  64.   
  65.     udp = (int)udp_open(0);
  66.     if(udp <= 0)
  67.     {
  68.         return(-1L);
  69.     }
  70.     
  71.     nm->nm_type = NI_NAME;
  72.     nm->nm_len  = (unsigned char)strlen(host);
  73.     strcpy(nm->nm_item,host);
  74.     
  75.     for(i=0;i < NM_TRIES;i++)
  76.     {
  77.          w_info(wp,"<Status: contacting name server.>");
  78.      if(udp_write(udp,buf,(unsigned int)nm->nm_len+2,&dest) <= 0)
  79.      {
  80.         udp_close(udp);
  81.         return(-1L);
  82.      }
  83.      timeout = clock() + CLK_TCK * NM_WAIT;    /* wait 10 sec */
  84.      do
  85.      {
  86.         evnt_timer(10,0);
  87.         got = udp_read(udp,buf,&dest);
  88.      } while(got == 0L && clock() < timeout);
  89.      if(got) break;
  90.     }
  91.     udp_close(udp);
  92.     if(got < 0L)
  93.     {
  94.         return(-1L);    /* error */
  95.     }
  96.     else if(!got)
  97.         return(-1L);            /* timeout */
  98.     else if(got > strlen(host)+2)
  99.     {
  100.         if(!strncmp(nm->nm_item, host, (size_t)nm->nm_len)) 
  101.         {            nm = (NMITEM *)((char *)nm + nm->nm_len + 2);            if(nm->nm_type == NI_ADDR) 
  102.             {                for(i=0; i < nm->nm_len; i++)
  103.                     ipaddr[i] = nm->nm_item[i];
  104.                 return(0L);        /* host found */
  105.             }
  106.         }    }
  107.     return(-1L);    /* host unknown */
  108. }